package com.clinic.accessor.db;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.clinic.entity.db.DrugBrand;
import com.clinic.entity.param.Index;

@Component
public class DrugBrandAccessor extends AbstractDbAccessor<DrugBrand> {
	
	public DrugBrand getDrugBrandById(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DrugBrand.class);
		criteria.add(Restrictions.eq(DrugBrand.Columns.BRAND_ID, id));
		return uniqueGeneric(criteria);
	}
	
	public List<DrugBrand> getDrugBrandsByName(String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DrugBrand.class);
		criteria.add(Restrictions.eq(DrugBrand.Columns.NAME, name));
		return listGeneric(criteria, new Index());
	}
	
	public DrugBrand getDrugBrandByName(String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DrugBrand.class);
		criteria.add(Restrictions.eq(DrugBrand.Columns.NAME, name));
		return uniqueGeneric(criteria);
	}
	
	public List<DrugBrand> getDrugBrands(Index index) {
		DetachedCriteria criteria = getCriteriaOfDrugBrands();
		return listGeneric(criteria, index);
	}
	
	public int countOfDrugBrands() {
		DetachedCriteria criteria = getCriteriaOfDrugBrands();
		return getCount(criteria);
	}
	
	public DetachedCriteria getCriteriaOfDrugBrands() {
		DetachedCriteria criteria = DetachedCriteria.forClass(DrugBrand.class);
		return criteria;
	}
	
	public List<DrugBrand> getOrderedDrugBrandsByTerm(String term, Index index) {
		DetachedCriteria criteria = getCriteriaOfOrderedDrugBrandsByTerm(term);
		return listGeneric(criteria, index);
	}
	
	public int countOfOrderedDrugBrandsByTerm(String term) {
		DetachedCriteria criteria = getCriteriaOfOrderedDrugBrandsByTerm(term);
		return getCount(criteria);
	}
	
	public DetachedCriteria getCriteriaOfOrderedDrugBrandsByTerm(String term) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DrugBrand.class);
		criteria.add(Restrictions.ilike(DrugBrand.Columns.NAME, "%" + term + "%"));
		return criteria;
	}
}
